<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /* 
          queryURLparams 获取URL地址传参的信息和哈希值  
            @params
                url[string]要解析的URL字符串
            @return
                [object]包含参数和哈希值信息的对象
         */
        let url = 'http://www.shidazhi.cn/index.html?1x=1&name=zhufeng&teacher=aaa#box'
        function queryURLParams(url) {
            let askIndex = url.indexOf('?')//获得?索引
            let wellIndex = url.indexOf('#')//获得#索引
            let askText = ''
            let wellText = ''
            //#号不存在
            if (wellIndex == -1) {
                wellIndex = url.length
            }
            //?号存在
            if (askIndex != -1) {
                askText = url.substring(askIndex + 1, wellIndex)//1x=1&name=zhufeng&teacher=aaa
                //如果有#号截取到1x=1&name=zhufeng&teacher=aaa
                //如果没有#号截取到1x=1&name=zhufeng&teacher=aaa
            }
            wellText = url.substring(wellIndex + 1)//box
            //如果#号存在截取到box
            //如果#号不存在截取完为''

            //获取每一部分信息
            let obj = {}
            if (wellText != '') {
                obj.HASH = wellText
            }
            if (askText != '') {
                let askAry = askText.split('&')// ['1x=1', 'name=zhufeng', 'teacher=aaa']
                askAry.forEach((item, index) => {
                    let n = item.split('=')
                    let j = n[0]
                    let k = n[1]
                    obj[j] = k
                })
            }
            return obj
        }
        console.log(queryURLParams(url));
    </script>
</body>

</html>